<?php namespace Phpcmf\Model\Quan;

// 自动执行方法
class Bisai extends \Phpcmf\Model
{

    
	/*比赛奖励积分*/
	public function jianglijifen($tablename, $dirname, $leixing) {
	    $tablezhongbiao = $tablename.'_bisaizhongbiao';
		$tablebisai = $tablename.'_form_bisai';
		$table = $tablename.'_form_son';
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$leixings = (int)\Phpcmf\Service::L('input')->get('leixing');
		$jianglis = (int)\Phpcmf\Service::L('input')->get('jiangli');
		$mingci = (int)\Phpcmf\Service::L('input')->get('mingci');
		$quanid = (int)\Phpcmf\Service::L('input')->get('quanid');
		$this->index = $this->db->table($table)->where('id', $id)->get()->getRowArray();
		if ($this->index['title']) {
			$datatitle = $this->index['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($this->index['neirong']), 70);
		}
		if ($leixings >= 4) {
			if (!$jianglis) {
				$msg = dr_string2array(['status' => 0,
					'value' => '奖励出错了',
					'jifen' => '',]);
				return dr_return_data(0, $msg);
			}
		}
		//dr_debug('$indexuid', $this->index);
	    if (!$this->uid) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '还没有登录',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    } elseif (!$id) {
			$msg = dr_string2array(['status' => 0,
			    'value' => 'id参数不完整',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    }
	
	    if (!$this->db->tableExists($table) ) {
	        return dr_return_data(0, dr_lang('没有比赛中标数据库表[%s]', $dirname));
	    }
		
		
		if ($this->index['uid'] != $this->uid) {
			
			$msg = dr_string2array(['status' => 0,
			    'value' => '您不是提问者，无权操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		} /*else {
			$msg = dr_string2array(['status' => 0,
			    'value' => '操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}*/
		//判断用户是否提交作品
		$shifoutijiao = $this->db->table($tablebisai)->where('tid', $id)->where('uid', $uid)->get()->getRowArray();
		if (!$shifoutijiao) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '没有这位用户的作品',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}
	
	    $jindu = $this->db->table($tablezhongbiao)->where('cid', $id)->where('leixing', $leixings)->where('uid', $uid)->get()->getRowArray();
	    if ($jindu) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '已奖励'.SITE_SCORE,
				'jifen' => '',]);
			return dr_return_data(1, $msg, $c);

	    } else {
			$val = 1;
			$rmb = 0.1;
			if ($leixings == '1') {
				//积分奖励
				if ($this->member['score'] < $val) {
					$msg = dr_string2array(['status' => 0,
					    'value' => '您的'.SITE_SCORE.'是 '.$this->member['score'].'已经不足奖励',
						'jifen' => '',]);
					return dr_return_data(0, $msg);
				}
				//减去发布问题者的积分
				\Phpcmf\Service::M('member')->add_score($this->uid, -$val, $note = '', $url = '', $mark = '', $count = 0);
				
				//给提交作品者增加积分
				\Phpcmf\Service::M('member')->add_score($uid, $val, $note = '', $url = '', $mark = '', $count = 0);
				//奖励积分提示
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',奖励 '.$val.' 积分',]);
			} elseif ($leixings == '2'){
				//人民币奖励
				if ($this->member['money'] < $rmb) {
					$msg = dr_string2array(['status' => 0,
					    'value' => '您的余额是 '.$this->member['money'].'已经不足奖励',
						'jifen' => '',]);
					return dr_return_data(0, $msg);
				}
				//减去发布问题者的金额
				\Phpcmf\Service::M('member')->add_money($this->uid, -$rmb);
				\Phpcmf\Service::M('Pay')->add_paylog([
				    'uid' => $this->uid,
				    'username' => "",
				    'touid' => "",
				    'tousername' => '',
				    'mid' => 'system', // 特征字符串，system表示系统
				    'title' => $datatitle,
				    'value' => -$rmb, // 交易金额，正数表示增加金额，负数表示消费金额
				    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				    'status' => 1, // 1表示交易成功,0表示未成功
				    'result' => $id."比赛奖励",
				    'paytime' => SYS_TIME, // 付款时间，默认当前时间
				    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
				]);
				//给提交作品者增加金额
				
				\Phpcmf\Service::M('member')->add_money($uid, $rmb);
				\Phpcmf\Service::M('Pay')->add_paylog([
				    'uid' => $uid,
				    'username' => "",
				    'touid' => "",
				    'tousername' => '',
				    'mid' => 'system', // 特征字符串，system表示系统
				    'title' => $datatitle,
				    'value' => $rmb, // 交易金额，正数表示增加金额，负数表示消费金额
				    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
				    'status' => 1, // 1表示交易成功,0表示未成功
				    'result' => $id."比赛奖励",
				    'paytime' => SYS_TIME, // 付款时间，默认当前时间
				    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
				]);
				//奖励积分提示
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',奖励 '.$rmb.' 人民币',]);
			} elseif ($leixings == '3'){
				//感谢参与
				$msg = dr_string2array(['status' => 1,
				    'value' => '操作成功',
					'jifen' => ',感谢参与',]);
				\Phpcmf\Service::M('member')->notice($uid, 1, '《感谢参与》'.$datatitle, $url = '', $mark = '');
			} elseif ($leixings == '4'){
				//中标
				if($this->index['jlfs'] == '1'){
					$rmb = $jianglis;
					\Phpcmf\Service::M('member')->add_score($this->uid, intval(-$rmb), $datatitle, $url = '', $mark = '', $count = 0);
					\Phpcmf\Service::M('member')->add_score($uid, intval($rmb), $datatitle, $url = '', $mark = '', $count = 0);
					$msg = dr_string2array(['status' => 1,
					    'value' => '操作成功',
						'jifen' => ',恭喜您中标获得 '.intval($rmb).SITE_SCORE,]);
				} else {
					//给提交作品者增加金额
					$rmb = $jianglis;
					\Phpcmf\Service::M('member')->add_money($uid, $rmb);
					\Phpcmf\Service::M('Pay')->add_paylog([
					    'uid' => $uid,
					    'username' => "",
					    'touid' => "",
					    'tousername' => '',
					    'mid' => 'system', // 特征字符串，system表示系统
					    'title' => $datatitle,
					    'value' => $rmb, // 交易金额，正数表示增加金额，负数表示消费金额
					    'type' => 'finecms', // 支付方式，alipy，weixin，finecms表示余额
					    'status' => 1, // 1表示交易成功,0表示未成功
					    'result' => $id."比赛奖励",
					    'paytime' => SYS_TIME, // 付款时间，默认当前时间
					    'inputtime' => SYS_TIME, // 流水参生时间，默认当前时间
					]);
					$msg = dr_string2array(['status' => 1,
					    'value' => '操作成功',
						'jifen' => ',恭喜您中标获得 '.$rmb.' 人民币',]);
				}
				
			}
			if($rmb == 0){
				$this->db->table($tablezhongbiao)->insert(array(
					'cid' => $id,
					'leixing' => $leixings,
					'shenhe' => 1,
					'jiangli' => 0,
					'mingci' => $mingci,
					'jianglirmb' => 0,
					'uid' => $uid,
					'inputtime' => SYS_TIME,
					'endtime' => SYS_TIME
				));
			}else{
				if($this->index['jlfs'] == '1'){
					$this->db->table($tablezhongbiao)->insert(array(
						'cid' => $id,
						'leixing' => $leixings,
						'shenhe' => 1,
						'mingci' => $mingci,
						'jiangli' => intval($rmb),
						'jianglirmb' => 0,
						'uid' => $uid,
						'inputtime' => SYS_TIME,
						'endtime' => SYS_TIME
					));
				} else {
					$this->db->table($tablezhongbiao)->insert(array(
						'cid' => $id,
						'leixing' => $leixings,
						'shenhe' => 1,
						'mingci' => $mingci,
						'jiangli' => 0,
						'jianglirmb' => $rmb,
						'uid' => $uid,
						'inputtime' => SYS_TIME,
						'endtime' => SYS_TIME
					));
				}
			}
				
			//更新
			//$this->db->table($table)->where('id', $id)->update(['jindu' => 1]);
	    }
		
	
	    // 更新缓存
	    //$c = $this->db->table($tablename.'_form_son_zan')->where('cid', $id)->countAllResults();
	    //$this->db->table($tablename.'_form_son')->where('id', $id)->set('zan', $c)->update();
	    \Phpcmf\Service::M('cache')->update_data_cache();
	
	    // 返回结果
	    return dr_return_data(1, $msg, $c);
	
	}
	/*比赛审核*/
	public function daanshenhe($tablename, $dirname, $leixing) {
	    $tablezhongbiao = $tablename.'_bisaizhongbiao';
		$tablebisai = $tablename.'_form_bisai';
		$table = $tablename.'_form_son';
	    $id = (int)\Phpcmf\Service::L('input')->get('id');
		$uid = (int)\Phpcmf\Service::L('input')->get('uid');
		$tid = (int)\Phpcmf\Service::L('input')->get('tid');
		$quanid = (int)\Phpcmf\Service::L('input')->get('quanid');
		$this->index = $this->db->table($table)->where('id', $tid)->get()->getRowArray();
		if ($this->index['title']) {
			$datatitle = $this->index['title'];
		} else {
			$datatitle = dr_strcut(dr_clearhtml($this->index['neirong']), 70);
		}
		//dr_debug('$indexuid', $this->index);
	    if (!$this->uid) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '还没有登录',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    } elseif (!$id) {
			$msg = dr_string2array(['status' => 0,
			    'value' => 'id参数不完整',
				'jifen' => '',]);
	        return dr_return_data(0, $msg);
	    }
	
	    if (!$this->db->tableExists($table) ) {
	        return dr_return_data(0, dr_lang('没有比赛中标数据库表[%s]', $dirname));
	    }
		
		
		if ($this->index['uid'] != $this->uid) {
			
			$msg = dr_string2array(['status' => 0,
			    'value' => '您不是提问者，无权操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		} /*else {
			$msg = dr_string2array(['status' => 0,
			    'value' => '操作'.$id,
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}*/
		//判断用户是否提交作品
		$shifoutijiao = $this->db->table($tablebisai)->where('id', $id)->where('uid', $uid)->get()->getRowArray();
		if (!$shifoutijiao) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '没有这位用户的作品',
				'jifen' => '',]);
			return dr_return_data(0, $msg, $c);
		}
		$zuopinshenhe = $this->db->table($tablebisai)->where('id', $id)->where('uid', $uid)->where('zuopinshenhe',1)->get()->getRowArray();
		if ($zuopinshenhe) {
			$msg = dr_string2array(['status' => 0,
			    'value' => '已审核',
				'jifen' => '',]);
			return dr_return_data(1, $msg, $c);
		} else {
			$this->db->table($tablebisai)->where('id', $id)->update(['zuopinshenhe' => 1]);
			$msg = dr_string2array(['status' => 0,
			    'value' => '已经审核成功',
				'jifen' => '',]);
			return dr_return_data(1, $msg, $c);
		}
		
	
	    // 更新缓存
	    //$c = $this->db->table($tablename.'_form_son_zan')->where('cid', $id)->countAllResults();
	    //$this->db->table($tablename.'_form_son')->where('id', $id)->set('zan', $c)->update();
	    \Phpcmf\Service::M('cache')->update_data_cache();
	
	    // 返回结果
	    return dr_return_data(1, $msg, $c);
	
	}

}